package com.nuanshui.heatedloan.baseservice.service;

import com.nuanshui.framework.cache.CacheConstants;
import com.nuanshui.framework.res.ResourceFacade;
import com.nuanshui.framework.res.qiniu.Qiniu;
import com.nuanshui.heatedloan.baseservice.dal.mapper.ImageMapper;
import com.nuanshui.heatedloan.baseservice.facade.ImageService;
import com.nuanshui.heatedloan.baseservice.vo.ImageFromFront;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.constant.TransactionManagerType;
import com.nuanshui.heatedloan.enums.FileBelong;
import com.nuanshui.heatedloan.model.Image;
import com.nuanshui.heatedloan.pageable.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;



@Service(ServiceConstant.IMAGESERVICE)
@Transactional(readOnly=true,  value = TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
public class ImageServiceImpl implements ImageService {

	@Autowired
	private ImageMapper imageMapper;
	@Autowired
	private Qiniu qiniu;

	@Autowired
	private ResourceFacade resourceFacade;

	@Override
	@Transactional(readOnly=false,value=TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
	public String insert(Image image) {
		imageMapper.insert(image);
		return image.getId();
	}

	public Pager<Image> list(Pager<Image> pager) {
		List<Image> list = imageMapper.list(pager);

		for (Image image: list) {
			handImage(image);
		}

		pager.setData(list);
		return pager;
	}

	@Override
	@Transactional(readOnly=false,value=TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
	public int delete(String id) {
		return imageMapper.deleteByPrimaryKey(id);
	}

	private void handImage(Image img)
	{
		if(img != null)
		{
			if(img.getCropX()==null && img.getCropY()==null)
			{
				img.setCropX(0D);
				img.setCropY(0D);
				Double cropWidth = img.getWidth() == null ? null : img.getWidth().doubleValue();
				Double cropHeight = img.getHeight()== null ? null : img.getHeight().doubleValue();
				img.setCropWidth(cropWidth);
				img.setCropHeight(cropHeight);
			}

			img.setImgUrl(resourceFacade.resolveUrl(img.getKey()));
		}
	}

	@Override
	@Cacheable(value = CacheConstants.IMAGE_ITEM,key="#id")
	public Image load(String id) {
		Image img=imageMapper.selectByPrimaryKey(id);
		handImage(img);
		return img;
	}

	@Override
	public Image loadByImgKey(String imgKey) {
		Image img=imageMapper.selectByImgKey(imgKey);
		handImage(img);
		return img;
	}

	@Override
	@Transactional(readOnly=false,value=TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
	public Image convertFrontImageToImage(ImageFromFront bannerImg) {
		if (bannerImg == null)
			return null;
		Image image = new Image();
		image.setBelong(FileBelong.PRODUCT);
		image.setCreatedAt(new Date());
		image.setHeight(Integer.parseInt(bannerImg.getHeight().split("\\.")[0]));
		image.setWidth(Integer.parseInt(bannerImg.getWidth().split("\\.")[0]));
		image.setCropX(Double.parseDouble(bannerImg.getCropx()));
		image.setCropY(Double.parseDouble(bannerImg.getCropy()));
		image.setCropWidth(Double.parseDouble(bannerImg.getCropWidth()));
		image.setCropHeight(Double.parseDouble(bannerImg.getCropHeight()));

		String bucketName = qiniu.genBucketName(FileBelong.PRODUCT);
		String key = qiniu.qiniuKeyToLocalKey(bannerImg.getImageKey(), bucketName);
		image.setKey(key);
		int isOK = imageMapper.insert(image);
		if (isOK > 0)
			return image;

		return null;
	}

	@Override
	public List<Image> findImageByIds(String[] ids) {
		return imageMapper.findImageByids(ids);
	}

}
